SpringBoot官方文档翻译(八):开始写代码

11.3 Writing the Code(开始写代码)

1
2
3
4
To finish our application, we need to create a single Java file.     
By default, Maven compiles sources from src/main/java, so you need
to create that folder structure and then add a file named 
src/main/java/Example.java to contain the following code:

为了完成我们的应用,我们需要创建一个单独的java文件夹。默认情况下,maven从src/main/java中编译源码,因此,您需要创建一个类似于此的文件结构目录并加入您的示例类src/main/java/Example.java并包含以下代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.web.bind.annotation.*;

@RestController
@EnableAutoConfiguration
public class Example {

@RequestMapping("/")
String home() {
return "Hello World!";
}

public static void main(String[] args) throws Exception {
SpringApplication.run(Example.class, args);
}

}
1
2
Although there is not much code here, quite a lot is going on.     
We step through the important parts in the next few sections.

尽管并没有多少代码,但很多事情静静的发生了。我们将在接下来的章节,带您一步一步解开它的神秘面纱。

11.3.1 The @RestController and @RequestMapping Annotations

1
2
3
4
5
The first annotation on our Example class is @RestController.     
This is known as a stereotype annotation. It provides hints
for people reading the code and for Spring that the class plays
a specific role. In this case, our class is a web @Controller,
so Spring considers it when handling incoming web requests.

在我们Example类的第一个注解是@RestController。这被认为是一个刻板的注解。它为大家提供了直接的提示,并且告诉Spring容器,被注解的这个类扮演了一个特殊的角色。在这个例子李,我们的类是一个web类型的@Controller,所以Spring会注意到这点,并将web请求交给该类来处理。

1
2
3
4
The @RequestMapping annotation provides “routing” information.     
It tells Spring that any HTTP request with the / path should be
mapped to the home method. The@RestController annotation tells
Spring to render the resulting string directly back to the caller.

@RequestMapping注解提供了“路由”信息。它告诉Sring,任何的HTTP以/结尾的请求需要映射到home方法上。@RestController注解告诉Spring将结果字符串直接返回给调用方。

1
2
3
The @RestController and @RequestMapping annotations are Spring     
MVC annotations. (They are not specific to Spring Boot.) See
the MVC sectionin the Spring Reference Documentation for more details.

@RestController和@RequestMapping这两个注解是Spring MVC的注解,并非专门为Spring Boot提供的。您可以到Spring官方文档的MVC相关的章节去获取更多的信息。

11.3.2 The @EnableAutoConfiguration Annotation

1
2
3
4
5
6
The second class-level annotation is @EnableAutoConfiguration.     
This annotation tells Spring Boot to “guess” how you want to
configure Spring, based on the jar dependencies that you have added.
Since spring-boot-starter-web added Tomcat and Spring MVC,
the auto-configuration assumes that you are developing a web
application and sets up Spring accordingly.

第二个类级别注解@EnableAutoConfiguration,这个注解告诉Spring Boot 去“猜”您是想怎样配置Spring,基于您已经添加的jar包依赖。因为spring-boot-starter-web包含了Tomcat和Spring MVC,auto-configuration自动配置会推断您想开发一个web应用并据此设定Spring相关配置。

1
2
3
4
5
Starters and Auto-Configuration
Auto-configuration is designed to work well with “Starters”, but
the two concepts are not directly tied. You are free to pick and
choose jar dependencies outside of the starters. Spring Boot still
does its best to auto-configure your application.

“Starters”和“Auto-Configuration”
自动配置的设计是为了更好的配合“Starters”运作,但是两个概念并不直接相关。您可以自由的挑选不在“Starters”中的jar依赖。Spring Boot依然尽最大努力为您的应用做自动配置。

11.3.3 The “main” Method (”main“方法)

1
2
3
4
5
6
7
8
9
The final part of our application is the main method. This is     
just a standard method that follows the Java convention for an
application entry point. Our main method delegates to Spring
Boot’s SpringApplication class by calling run. SpringApplication 
bootstraps our application, starting Spring, which, in turn,
starts the auto-configured Tomcat web server. We need to pass 
Example.class as an argument to the run method to tell 
SpringApplication which is the primary Spring component. The 
args array is also passed through to expose any command-line arguments.

本节的最后一部分介绍我们应用的”main“方法。这只是遵循Java约定的标准方法应用程序入口点。
我们的main方法会调用给Spring Boot的SpringApplication类的run方法。SpringApplication引导我们的应用程序,从Spring开始,启动自动配置的Tomcat Web服务器。我们需要将Example.class作为run方法的一个参数,去通知SpringApplication,这是Spring的主要组件。该参数数组同样可以通过任何暴露的命令行参数进行传递。

分享到